"""arkTS接口一致性校验扫描执行器"""
from typing import Dict, Any, List
from app.scanners.base import ScanExecutor, ScanContext, ScanResult, ScanException
from app.scanners.registry import ScanExecutorRegistry


@ScanExecutorRegistry.register("arkts_consistency_scan")
class ArkTSConsistencyScanExecutor(ScanExecutor):
    """arkTS接口一致性校验扫描执行器"""
    
    def get_task_type(self) -> str:
        return "arkts_consistency_scan"
    
    def validate_params(self, scan_params: Dict[str, Any]) -> bool:
        """验证扫描参数"""
        # 可以根据实际需求添加参数验证
        return True
    
    def execute_scan(self, context: ScanContext) -> ScanResult:
        """
        执行扫描
        
        注意：这是一个示例实现，实际应该调用外部扫描工具
        """
        # TODO: 实际实现中应该调用外部扫描工具
        # 例如：解析Excel文件（arkTS接口一致性校验扫描结果.xlsx）
        
        # 模拟扫描结果（实际应从外部工具获取）
        issues = self._simulate_scan(context)
        
        # 构建扫描结果
        return ScanResult(
            issues=issues,
            summary={
                "total_issues": len(issues),
                "critical_count": sum(1 for i in issues if i.get("level") == "critical"),
                "warning_count": sum(1 for i in issues if i.get("level") == "warning"),
                "info_count": sum(1 for i in issues if i.get("level") == "info"),
            },
            metadata={
                "check_type": context.scan_params.get("check_type", "interface_consistency"),
            }
        )
    
    def _simulate_scan(self, context: ScanContext) -> List[Dict[str, Any]]:
        """模拟扫描（实际实现中应调用真实扫描工具）"""
        # 这是一个示例实现，返回模拟数据
        # 实际实现应该：
        # 1. 调用外部扫描工具或读取扫描结果文件
        # 2. 解析结果（Excel、CSV等）
        # 3. 转换为标准格式，包含多代码片段
        
        return [
            {
                "rule_name": "arkTS与cangjie不一致",
                "level": "critical",
                "file_path": "/api/ohos.arkui.component.flex.cj.d",
                "line_number": 10,
                "description": "APILevel注解格式不一致：cangjie使用since字段，arkTS使用数字",
                "code_snippet": "@!APILevel[\n    22,\n    syscap: \"SystemCapability.ArkUI.ArkUI.Full\"\n]",
                # 多代码片段支持
                "cangjie_code_snippet": {
                    "language": "cangjie",
                    "code": "@!APILevel[\n    since: \"22\",\n    syscap: \"SystemCapability.ArkUI.ArkUI.Full\"\n]",
                    "label": "Cangjie代码"
                },
                "arkts_code_snippet": {
                    "language": "arkts",
                    "code": "@!APILevel[\n    22,\n    syscap: \"SystemCapability.ArkUI.ArkUI.Full\"\n]",
                    "label": "ArkTS代码"
                },
                "interface_name": "Flex",
                "interface_type": "class",
                "mismatch_type": "annotation_format",
            },
        ]
    
    def get_supported_extensions(self) -> List[str]:
        """返回支持的扩展字段"""
        return ["interface_name", "interface_type", "mismatch_type", "reference_interface", "cangjie_code_snippet", "arkts_code_snippet"]

